<?php


namespace app\pc\service\base;


use app\admin\model\base\Paper;
use app\admin\model\base\Paper as Question;
use app\admin\model\base\StudentPaper;
use app\admin\model\base\StudentPractise;
use app\BaseService;
use think\response\Json;

class PaperService extends BaseService
{
    /**
     * 列表
     * @param array $params
     * @return Json
     */
    public function index(array $params): Json
    {
        $where = ['is_del'=>0,'is_release'=>1];
        //获取列表
        $list = Paper::where('is_del','=',0)
            ->where('is_release','=',1)
            ->field('student,id')
            ->select()->toArray();
        $arr = [];
        $arr[] = 999999999;
        if(!empty($list)){
            foreach ($list as $value){
                if(is_string($value['student'])){
                    $a = json_decode($value['student']);
                    if(in_array(PC_USER_ID,$a)){
                        $arr[] = $value['id'];
                    }
                }
            }
        }
        $paperList = Paper::where('is_del','=',0)
            ->where('is_release','=',1)
            ->where('id','in',implode(',',$arr))
            ->with(['paper_subjects','special'])
            ->order('id','desc')
            ->paginate(['list_rows' => $params['limit'] ?? 10, 'page' => $params['page'] ?? 1], false)->toArray();
        if(!empty($paperList) && !empty($paperList['data'])){
            foreach ($paperList['data'] as $k=>$datum){
                $subject_num = count($datum['paper_subjects']);
                $subject_score = 0;
                if(!empty($item['paper_subjects'])){
                    foreach ($item['paper_subjects'] as $subject){
                        $subject_score+= $subject['weighting'];
                    }
                }
                $paperList['data'][$k] = [
                    'name'=>$datum['name'],
                    'end_time'=>$datum['end_time'],
                    'subject_num'=>$subject_num,
                    'subject_score'=>$subject_score,
                    'img_url'=>$datum['img_url'],
                    'special_name'=>$datum['special']['name'],
                ];
            }
        }
        return jsonReturn(1, $paperList);
    }

    /**
     * 列表
     * @param array $params
     * @return Json
     */
    public function lists(array $params): Json
    {
        $where = [];
        $where[] = ['student_id','=',PC_USER_ID];
        //我的考试
        $list = Paper::where('is_del','=',0)
            ->where('is_release','=',1)
            ->field('student,id')
            ->select()->toArray();
        $arrss = [];
        $arrss[] = 0;
        if(!empty($list)){
            foreach ($list as $value){
                if(is_string($value['student'])){
                    $a = json_decode($value['student']);
                    if(in_array(PC_USER_ID,$a)){
                        $arrss[] = $value['id'];
                    }
                }
            }
        }
        $where2 = [];
        $where2[] = ['is_del','=',0];
        $where2[] = ['is_release','=',1];
        if(isset($params['special_id']) && $params['special_id']){
            $where2[] = ['special_id','=',$params['special_id']];
        }
        if(isset($params['name']) && $params['name']){
            $where2[] = ['name','=',$params['name']];
        }
        //查询用户是否参加考试
        $list = StudentPaper::where($where)->select()->toArray();
        $arr = [];
        $ids = [0];//未参加
        $ids2 = [0];//考试中
        $ids3 = [0];//待阅分
        $ids4 = [0];//已完成
        if(!empty($list)){
            foreach ($list as $item){
                $arr[$item['paper_id']] = $item;
                $ids[] = $item['paper_id'];
                if($item['scoring'] == 1){
                    $ids4[] = $item['paper_id'];
                }else{
                    if($item['status'] == 0){
                        $ids2[] = $item['paper_id'];
                    }
                    if($item['status'] == 1){
                        $ids3[] = $item['paper_id'];
                    }
                }

            }
        }

        if(isset($params['status']) && $params['status'] == 0){
            //刷选未参加的
            $ids = array_values(array_unique($ids));
            $arrss = array_diff($arrss,$ids);
            if(empty($arrss)) $arrss = [0];
        }
        if(isset($params['status']) && $params['status'] == 1){
            //考试中
            $ids2 = array_values(array_unique($ids2));
            $arrss = array_intersect($arrss,$ids2);
            if(empty($arrss)) $arrss = [0];
        }
        if(isset($params['status']) && $params['status'] == 2){
            //待阅分
            $ids3 = array_values(array_unique($ids3));
            $arrss = array_intersect($arrss,$ids3);
            if(empty($arrss)) $arrss = [0];

        }
        if(isset($params['status']) && $params['status'] == 3){
            //已完成
            $ids4 = array_values(array_unique($ids4));
            $arrss = array_intersect($arrss,$ids4);
            if(empty($arrss)) $arrss = [0];
        }
        $where2[] = ['id','in',implode(',',$arrss)];
        $practiseList = Paper::where($where2)
            ->with(['paper_subjects'])
            ->order('id','desc')
            ->paginate(['list_rows' => $params['limit'] ?? 10, 'page' => $params['page'] ?? 1], false)->toArray();
        $arrs = [];
        if(!empty($practiseList['data'])){
            foreach ($practiseList['data'] as $k=>$datum){
                $datas = [];
                $datas['name'] = $datum['name'];
                $datas['status'] = 0;
                $datas['start_time'] = $datum['start_time'];
                $datas['end_time'] = $datum['end_time'];
                $datas['student_end_time'] = '--';
                $datas['score'] = '--';
                $datas['id'] = 0;
                $datas['paper_id'] = $datum['id'];
                $datas['subject_num'] = !empty($datum['paper_subjects']) ? count($datum['paper_subjects']) : 0;
                $datas['subject_score'] = !empty($datum['paper_subjects']) ? array_sum(array_column($datum['paper_subjects'], 'weighting')) : 0;
                $practiseList['data'][$k]['paper'] = $arr[$datum['id']] ?? [];
                if(isset($arr[$datum['id']])){
                    $datas['id'] = $arr[$datum['id']]['id'];

                    if($arr[$datum['id']]['scoring'] == 1){
                        $datas['status'] = 3;//已判分
                        $datas['score'] = $arr[$datum['id']]['score'];
                        $datas['student_end_time'] = $arr[$datum['id']]['end_time'];
                    }else{
                        if($arr[$datum['id']]['status'] == 0){
                            $datas['status'] = 1;//考试中
                        }elseif($arr[$datum['id']]['status'] == 1){
                            $datas['status'] = 2;//阅卷中
                            $datas['student_end_time'] = $arr[$datum['id']]['end_time'];
                        }
                    }
                }
                $arrs[] = $datas;
            }
        }
        $practiseList['data'] = $arrs;
        return jsonReturn(1, $practiseList);
    }

    /**
     * 详情
     * @param array $params
     * @return Json
     */
    public function read(array $params): Json
    {
        $where = ['id'=>$params['id']];
        $info = Question::where($where)->with(['paper_subjects','special'])->find();
        if($info){
            $info = $info->toArray();
            $info['count_score'] = 0;
            if(!empty($info['paper_subjects'])){
                foreach ($info['paper_subjects'] as $k=>$item){
                    unset($info['paper_subjects'][$k]['analysis']);
                    unset($info['paper_subjects'][$k]['analysis_desc']);
                    $arr = json_decode($item['content'],true);
                    if($item['type'] == 1 || $item['type'] == 2){
                        if(isset($arr['list']) && !empty($arr['list'])){
                            foreach ($arr['list'] as $kk=>$value){
                                unset($arr['list'][$kk]['is_answer']);
                            }
                        }
                    }
                    $info['count_score']+= $item['weighting'];
                    $info['paper_subjects'][$k]['content'] = $arr;
                }
            }
        }
        return jsonReturn(1, $info);
    }
}